package com.terminus.test.split;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SplitUtil {
    private static Map<Integer,SplitModel> modelMap=new ConcurrentHashMap<>();

    public static SplitModel getOptimalModel(int k){
        initialize();
        if(modelMap.containsKey(k))
            return  modelMap.get(k);
        else
            return modelMap.get(1);
    }

    private static void initialize(){
        if(modelMap.isEmpty()){
            for (double i = 1; i <=32 ; i++) {
                if(i==1){
                    SplitModel model=new SplitModel();
                    model.setK(1);
                    model.setN(32);
                    model.setLap(0);
                    modelMap.put(1,model);
                }else {
                    for (double j = (i+31)/i; ((int)j) <=64/(i+1); j++) {
                        int n= (int) j;
                        int lap= (int) (((i*n)-32)/(i-1));
                        if(lap*(i-1)==(i*n)-32){

                            if(modelMap.containsKey((int)i)){
                                SplitModel model=modelMap.get((int)i);
                                if(model.getLap()<lap){
                                    model.setK((int)i);
                                    model.setN(n);
                                    model.setLap(lap);
                                    modelMap.put((int)i,model);
                                }
                            }else {
                                SplitModel model=new SplitModel();
                                model.setK((int)i);
                                model.setN(n);
                                model.setLap(lap);
                                modelMap.put((int)i,model);
                            }
                        }

                    }
                }

            }
        }
    }
}
